package com.lighting.smssystem.utils;

import com.lighting.smssystem.constrants.UserRole;
import com.lighting.smssystem.model.User;

import java.util.Optional;

/**
 * Author LeeMaster
 */
public class ThreadContext {

    private static final ThreadLocal<User> threadLocal = new ThreadLocal<>();

    public static void set(User user){
        threadLocal.set(user);
    }

    public static UserRole getRole(){
        return Optional.<UserRole>ofNullable(threadLocal.get().getUserRole())
                .orElse(UserRole.COMMON);
    }

    public static String getName(){
        return Optional.ofNullable(threadLocal.get().getUserChname()).orElse("测试或匿名用户");
    }

    public  static Long getId(){
        return Optional.ofNullable(threadLocal.get().getUserId()).orElse(-1L);
    }

}
